


#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvTIMER.h"   		//<DEBUG>

#include "ir_remo_ctrl.h"
#include "usb_vcom.h"

#include "command_test.h"

void OnInformation(uint8_t channel, param_t param)
{
	usb_printf("system core clock = %d\r\n", SystemCoreClock);
	usb_printf("cycle per us = %d\r\n", CyclesPerUs);
	usb_printf("stclk = %X, hclk = %X\r\n",
		SYSCLK->CLKSEL0.STCLK_S, SYSCLK->CLKSEL0.HCLK_S);
}

int g_idle = 1;
int g_power_down = 1;

void PowerDownTest(uint8_t ch, param_t param)
{
	switch (ch)
	{
	case 0:		// disable power down and idle
		g_idle = 0;
		g_power_down = 0;
		usb_printf("disable idle.\r\n");
		break;

	case 1:		// disable power down, enable idle only
		g_idle = 1;
		g_power_down = 0;
		usb_printf("enable idle.\r\n");
		break;

	case 2:		// enable power down
		g_idle = 1;
		g_power_down = 1;
		usb_printf("enable power down.\r\n");
		break;

	default:
		usb_printf("!02 illeagle param\r\n");
	}
}


void timer1_callback(uint32_t data)
{
	static int led=0;
	if ( 0 == led ) 	DrvGPIO_ClrBit(E_GPB, 2);				//<DEBUG> turn on LED
	else				DrvGPIO_SetBit(E_GPB, 2);
	led ^= 1;
}

void SetTimer0(uint8_t ch, param_t param)
{
	static uint32_t event=0;

	if ( 1==ch)
	{
		DrvSYS_SelectIPClockSource(E_SYS_TMR1_CLKSRC, 1);
		DrvTIMER_Open(E_TMR1, 512, E_PERIODIC_MODE);

		event = DrvTIMER_SetTimerEvent(E_TMR1, param, timer1_callback, 0);
		DrvTIMER_EnableInt(E_TMR1);
		DrvTIMER_Start(E_TMR1);
		usb_printf("Start timer 0 in %d\r\n", param);
	}
	else
	{
		DrvTIMER_DisableInt(E_TMR1);
		DrvTIMER_ClearTimerEvent(E_TMR1, event);
		DrvTIMER_Close(E_TMR1);
		usb_printf("Timer 0 in %d\r\n", param);
	}
}


void test_timer_callback(void)
{
	static int led=0;
	if ( 0 == led ) 	DrvGPIO_ClrBit(E_GPB, 2);				//<DEBUG> turn on LED
	else				DrvGPIO_SetBit(E_GPB, 2);
	led ^= 1;
}

void OnTimerTest(uint8_t ch, param_t param)
{
	static OS_TCID	timer_id = 0xFF;
	StatusType rs;

	if ( 0xFF == timer_id )
	{
		timer_id = CoCreateTmr(TMR_TYPE_PERIODIC,
			100, 1000, test_timer_callback);
		if ( 0xFF == timer_id ) usb_printf("Create timer failed.\r\n");
		else usb_printf("timer created %d\r\n", timer_id);
	}
	if ( 0xFF != timer_id )
	{
		if ( 1 == ch )
		{
			rs = CoStartTmr(timer_id);
			usb_printf("timer %d started, rs= 0x%02X\r\n", timer_id, rs);
		}
		else
		{
			rs = CoStopTmr(timer_id);
			usb_printf("timer %d stopped, rs= 0x%02X\r\n", timer_id, rs);
		}
	}
}



void OnSetGpio(uint8_t ch, param_t param)
{
	if (ch)		DrvGPIO_SetBit(E_GPB, 2);
	else		DrvGPIO_ClrBit(E_GPB, 2);
}










